package com.wholesale;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class WebViewLayout extends LinearLayout implements OnClickListener {
	private ViewFlipper viewFlipper;
	private Context context;
	private View baseView;
	private ListView bodyList;
	private Button leftButton;
	private Button rightButton;
	private TextView headView;
	private RelativeLayout loadingLayout;
	public MyWebViewClient webViewClient=new MyWebViewClient();  
	private WebView webView ;

	// 构造函数
	public WebViewLayout(Context context, final ViewFlipper viewFlipper,
			String leftName, String headName,String url) {
		super(context);
		this.viewFlipper = viewFlipper;
		this.context = context;
		// 加载需要的属性，加载方法一的子Layout
		baseView = ((Activity) getContext()).getLayoutInflater().inflate(
				R.layout.web, this);
		leftButton = (Button) baseView.findViewById(R.id.leftBtn);
		leftButton.setText(leftName);
		leftButton.setOnClickListener(this);
		rightButton = (Button) baseView.findViewById(R.id.rightBtn);
		rightButton.setVisibility(View.INVISIBLE);
		headView = (TextView) baseView.findViewById(R.id.title);
		headView.setText(headName);
		loadingLayout = (RelativeLayout)baseView.findViewById(R.id.loadingLayout);
		webView = (WebView)baseView.findViewById(R.id.webView);
		webView.getSettings().setJavaScriptEnabled(true); 
		webView.loadUrl(url);
		webView.setWebViewClient(webViewClient);
	}
	
	public class MyWebViewClient extends WebViewClient{  
	    @Override  
	    public boolean shouldOverrideUrlLoading(WebView view, String url) {  
	    	loadingLayout.setVisibility(View.VISIBLE);
	    	view.loadUrl(url);
	        return true;  
	    }  
	    @Override  
	    public void onPageFinished(WebView view, String url) {  
	    	loadingLayout.setVisibility(View.GONE);
	    }  
	}  
	  
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.leftBtn:
			viewFlipper.showPrevious();
			viewFlipper.removeView(this);
			break;
		case R.id.rightBtn:

			break;
		default:
			break;
		}

	}
}
